package com.zzb.framework.modules.dict.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzb.framework.cache.CacheComponent;
import com.zzb.framework.common.*;
import com.zzb.framework.modules.dict.entity.SysDict;
import com.zzb.framework.modules.dict.form.SysDictSearchForm;
import com.zzb.framework.modules.dict.service.SysDictService;
import com.zzb.framework.modules.role.controller.AbstractController;
import com.zzb.framework.modules.role.entity.SysUserEntity;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
* 数据字典controller
*/
@Controller
@RequestMapping("/framework/sysDict")
public class SysDictController extends AbstractController {
	private static final Logger logger = LoggerFactory.getLogger(SysDictController.class);
	@Autowired
	private SysDictService sysDictService;
	@Autowired
	private CacheComponent cacheComponent;

	/**数据字典管理首页*/
	@RequestMapping({ "", "/", "/index" })
	public String index() {
		return "framework/sysDict/index";
	}

	/**数据字典列表json*/
	@RequestMapping("list")
	@ResponseBody
	public BasePage<SysDict> list(SysDictSearchForm sysDictSearchForm) {
		sysDictSearchForm.setOrderBy("param_key asc , param_order asc");
		return sysDictService.queryPage(sysDictSearchForm);
	}

	/**数据字典表单页面*/
	@RequestMapping("form")
	public String form(String id, Model model) {
		SysDict sysDict;
		if (StringUtils.isEmpty(id)) {
			sysDict = new SysDict();
		} else {
			sysDict = sysDictService.get(id);
			if (sysDict == null)
				sysDict = new SysDict();
		}
		model.addAttribute("sysDict", sysDict);
		return "framework/sysDict/form";
	}

	/**数据字典保存*/
	@RequestMapping("/save")
	@ResponseBody
	public BaseResponse<SysDict> save(SysDict sysDict, HttpServletRequest request) {
		try {
			SysUserEntity user = getUser();
			return sysDictService.saveSysDict(sysDict, user);
		} catch (FrameworkException e) {
			e.printStackTrace();
			return new BaseResponse<>(BaseResponse.FAIL, e.getMessage());
		}
	}

	/**数据字典删除*/
	@RequestMapping("/delete")
	@ResponseBody
	public BaseResponse delete(String id, HttpServletRequest request) {
		try {
			return sysDictService.deleteSysDict(id, getUser());
		} catch (FrameworkException e) {
			e.printStackTrace();
			return new BaseResponse(BaseResponse.FAIL, e.getMessage());
		}
	}

	/**数据字典开启*/
	@RequestMapping("/open")
	@ResponseBody
	public BaseResponse open(String id, HttpServletRequest request) {
		try {
			return sysDictService.recoveryById(id, getUser());
		} catch (FrameworkException e) {
			e.printStackTrace();
			return new BaseResponse(BaseResponse.FAIL, e.getMessage());
		}
	}

	/**数据字典屏蔽*/
	@RequestMapping("/deny")
	@ResponseBody
	public BaseResponse deny(String id, HttpServletRequest request) {
		try {
			return sysDictService.trashById(id, getUser());
		} catch (FrameworkException e) {
			e.printStackTrace();
			return new BaseResponse(BaseResponse.FAIL, e.getMessage());
		}
	}

	@RequestMapping("/rawJson")
	@ResponseBody
	public BaseResponse<Map> rawJson() {
		if (!cacheComponent.exist("DICT", "json", Map.class)) {
			Map map = sysDictService.generatorJSON();
			cacheComponent.put("DICT", "json", map, 0L);
		}
		return new BaseResponse<>(BaseResponse.SUCCESS, "获取数据字典JSON成功", cacheComponent.get("DICT", "json", Map.class));
	}

	@RequestMapping("/downloadjson")
	public ResponseEntity<byte[]> downloadJson(HttpServletRequest request) throws IOException {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		headers.setContentDispositionFormData("attachment", "dict.js");
		String tempFileName = request.getServletContext().getRealPath("/") + "dict.js";

		BaseResponse<File> ret = sysDictService.generatorJSONFile(tempFileName);
		return new ResponseEntity<>(FileUtils.readFileToByteArray(ret.getContent()), headers, HttpStatus.CREATED);
	}

}
